package org.zjx.strategy.impl;

import org.zjx.command.ICommand;
import org.zjx.command.basic.DelayCommand;
import org.zjx.command.composite.FindImageCommand;
import org.zjx.command.login.*;
import org.zjx.command.service.AccountCommand;
import org.zjx.core.IWindow;
import org.zjx.core.impl.LoginWindow;
import org.zjx.state.StateManager;
import org.zjx.strategy.IStrategy;

import java.awt.*;
import java.awt.image.BufferedImage;


public class LoginStrategy implements IStrategy {

    @Override
    public ICommand analyze(IWindow window, BufferedImage screenshot, StateManager stateManager) {
        if (stateManager.getAccountState() == null || stateManager.getAccountState().getAccountName() == null) {
            return new AccountCommand(window, stateManager);
        }

        if (LoginServerCommand.validate(window, screenshot)) {
            return new LoginServerCommand(window, stateManager);
        }

        if (LoginHomeCommand.validate(window, screenshot)) {
            return new LoginHomeCommand(window, stateManager);
        }
        if (LoginToggleCommand.validate(window, screenshot)) {
            return new LoginToggleCommand(window, stateManager);
        }

        if (LoginAccountCommand.validate(window, screenshot)) {
            return new LoginAccountCommand(window, stateManager);
        }

        if (LoginRoleCommand.validate(window, screenshot)) {
            return new LoginRoleCommand(window, stateManager);
        }

        if (LoginCreateCommand.validate(window, screenshot)) {
            return new LoginCreateCommand(window, stateManager);
        }

        return new DelayCommand(window, 1000);
    }

    @Override
    public String getStrategyName() {
        return "登录策略";
    }

    @Override
    public boolean isApplicable(IWindow window, BufferedImage screenshot, StateManager stateManager) {
        return window instanceof LoginWindow;
    }

    private boolean findImage(IWindow window, BufferedImage screenshot, BufferedImage targetImage, Rectangle rectangle) {
        FindImageCommand findImageCommand = new FindImageCommand(window, screenshot, targetImage, rectangle);
        findImageCommand.execute();
        return findImageCommand.getPoint() != null;
    }
}
